import PySide6
from PySide6 import QtCore
from PySide6.QtWidgets import QGraphicsItem
from PySide6.QtCore import QPointF, QRectF, QSize, QObject
from PySide6.QtGui import Qt, QPen, QPixmap

'''
缩放图形项， 作为控制点。继QtCore承QObject主要是为使用信号和槽 
'''
class ScaleCtrlItem(QGraphicsItem, QObject):

    scaleSignal = QtCore.Signal(QRectF, bool)   # 缩放。信号对象.参数分别为：鼠标移动的矩形范围，缩放操作是否结束)

    '''构造函数， radius为图形项的半径'''
    def __init__(self, radius, parent = None):
        QGraphicsItem.__init__(self, parent)
        QObject.__init__(self, None)
        self.__size = QSize(18, 18)  #图形项的大小
        self.__bgImage = QPixmap("./image/scale.png")
        self.__startPos = None   # 鼠标按下的位置（场景坐标） 
        self.setFlag(QGraphicsItem.ItemIsFocusable)   # 让图形项可获取焦点
        self.setFlag(QGraphicsItem.ItemIsSelectable)
        if(parent != None):
            self.scaleSignal.connect(parent.scaleItemSlot)
    ''' 重写 boundingRect 函数'''
    def boundingRect(self) -> PySide6.QtCore.QRectF:
        margin = 1   # 裕量，上下左右四个边界都留出一些裕量，方便绘制图形(特别是轮廓)
        return QRectF(0 - self.__size.width() / 2.0 - margin, 0 - self.__size.height() / 2.0 - margin, 
                      self.__size.width() + 2 * margin, self.__size.height() + 2 * margin)

    '''重写 paint 函数'''
    def paint(self, painter: PySide6.QtGui.QPainter, option: PySide6.QtWidgets.QStyleOptionGraphicsItem, widget) -> None:
        pen = QPen()
        pen.setWidth(1)
        pen.setColor(Qt.red)
        pen.setStyle(Qt.DashLine)  # 虚线
        painter.setPen(pen)
        # 绘制椭圆外形
        painter.drawEllipse(self.rect().x(), self.rect().y(), self.__size.width(), self.__size.height() )
        # 绘制背景图
        painter.drawPixmap(self.rect().x(), self.rect().y(), 
                           self.rect().width(), self.rect().height(), self.__bgImage)

    '''重写鼠标按下事件'''
    def mousePressEvent(self, event: PySide6.QtWidgets.QGraphicsSceneMouseEvent) -> None:
        if self.__startPos == None :
            self.__startPos = event.scenePos() # 刚开始按下，记录按下的位置
            # print(self.__startPos)
        return QGraphicsItem.mousePressEvent(self, event)

    '''重写鼠标移动事件'''
    def mouseMoveEvent(self, event: PySide6.QtWidgets.QGraphicsSceneMouseEvent) -> None:
        if self.__startPos != None :
            curPos = event.scenePos()   # 当前鼠标的坐标
            delta = QRectF(self.__startPos.x(), self.__startPos.y(),
                           curPos.x() - self.__startPos.x(),
                           curPos.y() - self.__startPos.y() )
            # print(delta)
            self.scaleSignal.emit(delta, False)

        return QGraphicsItem.mouseMoveEvent(self, event)

    '''重写鼠标释放事件'''
    def mouseReleaseEvent(self, event: PySide6.QtWidgets.QGraphicsSceneMouseEvent) -> None:
        if self.__startPos != None :
            curPos = event.scenePos()   # 当前鼠标的坐标
            delta = QRectF(self.__startPos.x(), self.__startPos.y(),
                           curPos.x() - self.__startPos.x(),
                           curPos.y() - self.__startPos.y() )
            self.scaleSignal.emit(delta, True)
        self.__startPos = None # 松开鼠标，则图形项缩放完成
        
        return QGraphicsItem.mouseReleaseEvent(self, event)

    '''获取图形项矩形'''
    def rect(self) -> PySide6.QtCore.QRectF:
        return QRectF(0 - self.__size.width() / 2.0 , 0 - self.__size.height() / 2.0 , 
                      self.__size.width() , self.__size.height() )

